package com.youlai.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ArchivesMapper;
import com.youlai.boot.system.service.ArchivesService;
import com.youlai.boot.system.model.entity.Archives;
import com.youlai.boot.system.model.form.ArchivesForm;
import com.youlai.boot.system.model.query.ArchivesQuery;
import com.youlai.boot.system.model.vo.ArchivesVO;
import com.youlai.boot.system.converter.ArchivesConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 博客文章服务实现类
 *
 * @author youlaitech
 * @since 2025-03-15 19:25
 */
@Service
@RequiredArgsConstructor
public class ArchivesServiceImpl extends ServiceImpl<ArchivesMapper, Archives> implements ArchivesService {

    private final ArchivesConverter archivesConverter;

    /**
     * 获取博客文章分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ArchivesVO>} 博客文章分页列表
     */
    @Override
    public IPage<ArchivesVO> getArchivesPage(ArchivesQuery queryParams) {
        Page<Archives> pageVO = this.baseMapper.getArchivesPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        List<ArchivesVO> archivesVOS = pageVO.getRecords().stream().map(archivesConverter::toArchivesVo).collect(Collectors.toCollection(ArrayList::new));
        Page<ArchivesVO> archivesVOPage = new Page<>(pageVO.getCurrent(), pageVO.getSize());
        archivesVOPage.setRecords(archivesVOS);
        return archivesVOPage;
    }

    /**
     * 获取博客文章表单数据
     *
     * @param id 博客文章ID
     * @return
     */
    @Override
    public ArchivesForm getArchivesFormData(Long id) {
        Archives entity = this.getById(id);
        return archivesConverter.toForm(entity);
    }

    /**
     * 新增博客文章
     *
     * @param formData 博客文章表单对象
     * @return
     */
    @Override
    public boolean saveArchives(ArchivesForm formData) {
        Archives entity = archivesConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新博客文章
     *
     * @param id   博客文章ID
     * @param formData 博客文章表单对象
     * @return
     */
    @Override
    public boolean updateArchives(Long id,ArchivesForm formData) {
        Archives entity = archivesConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除博客文章
     *
     * @param ids 博客文章ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteArchivess(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的博客文章数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    /**
     * 返回最近发布的3篇文章
     * @return
     */
    @Override
    public ArrayList<Archives> getArchivesTop3() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("publish_time");
        queryWrapper.last("limit 3");
        return (ArrayList<Archives>) this.list(queryWrapper);
    }

    @Override
    public Archives getArchivesById(Long id) {
        return this.getById(id);
    }

}
